package com.marry.intelli.plugin;

import com.intellij.ide.util.PropertiesComponent;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.SimpleToolWindowPanel;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowFactory;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiFile;
import com.intellij.psi.impl.file.PsiDirectoryFactory;
import com.intellij.psi.impl.source.xml.XmlFileImpl;
import com.intellij.psi.xml.XmlFile;
import com.intellij.psi.xml.XmlTag;
import com.intellij.ui.content.Content;
import com.intellij.ui.content.ContentFactory;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.util.*;

/**
 * @author boyfighting@qq.com
 * @desc
 * @date 2019/4/26
 */
public class SearchSqlToolWindowFactory implements ToolWindowFactory {
    @Override
    public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindow toolWindow) {
        toolWindow.setToHideOnEmptyContent(false);

        SimpleToolWindowPanel panel = new SimpleToolWindowPanel(false, true);


        panel.setContent(getPanelContainer(project));
        Content content = ContentFactory.SERVICE.getInstance().createContent(panel, "", false);
        content.setCloseable(true);
        toolWindow.getContentManager().addContent(content);
    }

    private JPanel getPanelContainer(Project project) {

        PsiDirectory baseDir = PsiDirectoryFactory.getInstance(project).createDirectory(project.getBaseDir());
        System.out.println(baseDir.getParent());
        List<PsiFile> psiFileList = new ArrayList<>();
        showAndCollectFiles(baseDir.getParent(), psiFileList);
        Map<String, XmlTag> sqlMap = new HashMap<String, XmlTag>();
        for (PsiFile psiFile : psiFileList) {
            if (psiFile instanceof XmlFile) {
                XmlFile xmlFile = (XmlFileImpl) psiFile;
                XmlTag rootTag = xmlFile.getRootTag();
                if (rootTag == null) {
                    continue;

                }
                if (!"sqlMap".equals(rootTag.getName())) {
                    continue;
                }
                String namespace = rootTag.getAttributeValue("namespace");
                for (XmlTag child : rootTag.getSubTags()) {
                    if (!"sql".equals(child.getName())) {
                        continue;

                    }
                    String sqlId = child.getAttributeValue("id");

                    String mapKey = namespace + "." + sqlId;
                    sqlMap.put(mapKey, child);
                }

                rootTag.getSubTags();
            }


        }
        System.out.println(psiFileList);
        System.out.println(sqlMap);

        SearchDiaog searchDiaog = new SearchDiaog();
        searchDiaog.setTitle("查询SQL");
        searchDiaog.setContextText("");
        searchDiaog.setResizable(true);
        searchDiaog.setProject(project);
        searchDiaog.setSqlMap(sqlMap);
        return searchDiaog.getContainer();
    }


    private void showAndCollectFiles(PsiDirectory baseDir, List<PsiFile> psiFileList) {
        PsiDirectory[] resources = baseDir.getSubdirectories();
        if (resources.length == 0) {
            PsiFile[] psiFiles = baseDir.getFiles();
            psiFileList.addAll(Arrays.asList(psiFiles));

        }
        for (PsiDirectory directory : resources) {
            if (!directory.isDirectory()) {
                PsiFile[] psiFiles = baseDir.getFiles();
                psiFileList.addAll(Arrays.asList(psiFiles));


            } else {
                showAndCollectFiles(directory, psiFileList);
            }
        }

    }
}
